var calendars = []; // all calendars 
var mounth_str = ['январь','февраль','март','апрель','май','июнь','июль','август','сентябрь','октябрь','ноябрь','декабрь'];
var day1, day2, day3;






function calendar_create(atarget)
{
    if (!atarget) return null;// cal_error("Ошибка создания календаря: не задано поле ввода");
    if (atarget.value == null) return null;//cal_error("Ошибка создания календаря: задано неверное поле ввода");
	this.target=atarget;
	this.popup=calendar_popup;
	this.id = calendars.length;
	calendars[this.id]=this;	
	return this;
}

function cal_error(mes)
{
	alert(mes);
	return null;
}

function writeday(doc, anid, y, m, d, dow, can_set)
{
	mm=1*m; if (mm<10) mm="0"+mm;
	dd=1*d; if (dd<10) dd="0"+dd;
	if (can_set)
	{
		doc.write('\r\n<A href="#" onclick="javascript:opener.calendars['+anid+'].target.value=\''+dd+'.'+mm+'.'+y+'\'; window.close(); opener.focus();  opener.calendars['+anid+'].target.parent.submit();  return false;"><b> '+d+' </b></a>');
	}
	else
	{
		doc.write('\r\n<b> '+d+' </b>');	
	}
}

function forwardback(doc,anid,y,m,txt)
{
	ms=1*m;
	if (ms>12) { ms=1; y++; }
	if (ms<1) { ms=12; y--; }
	if (ms<10) ms="0"+ms;
	doc.write('\r\n<td bgcolor=silver><A href="#" onclick="javascript:opener.calendars['+anid+'].target.value=\'01.'+ms+'.'+y+'\'; opener.calendars['+anid+'].popup(); return false;">'+txt+'</a></td>');
}

function calendar_popup(fromx)
{
	var obj_calwindow =0;
	var str_date = this.target.value;
    var arr_date = str_date.split('-');
	if (arr_date.length != 3) { arr_date = str_date.split('.'); };	
    if (arr_date.length != 3) { alert("Неверный формат даты: '" + str_date + "'.\nДопустимый формат ДД.ММ.ГГ или ДД-ММ-ГГГГ"); return; };
	obj_calwindow= window.open('','Календарь','width=190,height=200,status=no,location=no,resizable=yes,top=200,left=220,dependent=yes,alwaysRaised=yes');
	dd=1*arr_date[0];
	mm=1*arr_date[1];
	yy=1*arr_date[2];
	if (yy<1900) yy=yy+1900;
	if (mm>12) mm=12;
	var tempdate = new Date(yy,mm-1,1);
	var curdate = new Date();
	wdstart=tempdate.getDay(); // ## dnya nedeli v 1 den mesyatsa	
	if (wdstart==0) wdstart=7;
	
	doc=obj_calwindow.document;
	doc.clear(); // make it empty !!!
	doc.write('<HTML><HEAD><META http-equiv=Content-Type content="text/html; charset=utf-8"><TITLE>Календарь</TITLE><STYLE>');
	//*************************************	
	doc.write(' HTML, BODY, TABLE, A {margin: 0; padding: 0}');	
	doc.write(' TABLE { margin: 0; padding: 0}'); 
	doc.write(' TD {border: 1px; bordercolor:Black; margin: 5px; padding: 5px; COLOR: #000000; FONT: 10px verdana,arial; text-align: center}');
	doc.write(' A:link {FONT: 10px verdana,arial; COLOR: #06c000; TEXT-DECORATION: none}');
	doc.write(' A:visited {FONT: 10px verdana,arial; COLOR: #039000; TEXT-DECORATION: none}');
	doc.write(' A:hover {FONT: 10px verdana,arial; COLOR: #d57919; TEXT-DECORATION: underline}');	
	//*************************************	
	doc.write('</STYLE></HEAD><BODY>');
	doc.write('\r\n<table align=center border=1 cellspacing=2 cellpadding=2><tr>');
	forwardback(doc,this.id,1*yy,1*mm-1,"&lt;&lt;");
	doc.write('<td colspan=5>'+mounth_str[mm-1]+' '+yy+'</td>');
	forwardback(doc,this.id,1*yy,1*mm+1,"&gt;&gt;");
	doc.write('<tr bgcolor=#ADD8E6><td>пн<td>вт<td>ср<td>чт<td>пт<td>сб<td>вс</tr>');
	day=1;
	week=1;
	started=0;	
	
	
	day1 = new Date();
	day2= new Date();
	day3= new Date();	
	
	msPerDay = 24 * 60 * 60 * 1000 ; // Number of milliseconds per day
	day1.setTime(curdate.getTime() - 1 * msPerDay);	
	//first day
	is_day_set = false;	
	while( ! is_day_set)
	{
		if ((day1.getDay() == 0) || (day1.getDay() == 6))
		{
			day1.setTime(day1.getTime() - 1 * msPerDay);		
		}
		else 
		{
			is_day_set = true;
		}	
	}	
	//second day
	day2.setTime(day1.getTime() - 1 * msPerDay);
	is_day_set = false;	
	while( ! is_day_set)
	{
		if ((day2.getDay() == 0) || (day2.getDay() == 6))
		{
			day2.setTime(day2.getTime() - 1 * msPerDay);		
		}
		else 
		{
			is_day_set = true;
		}	
	}	
	//third day
	is_day_set = false;	
	day3.setTime(day2.getTime() - 1 * msPerDay);
	while( ! is_day_set)
	{
		if ((day3.getDay() == 0) || (day3.getDay() == 6))
		{
			day3.setTime(day3.getTime() - 1 * msPerDay);		
		}
		else 
		{
			is_day_set = true;
		}	
	}
	
	
	
	
	
	for (week = 1; week < 7; week++)
	{
		doc.write('<tr>');
		for (weekday = 0; weekday <= 6; weekday++) {
			wd = weekday + 1;
			sdow = '';	
			
			var can_set = false;		
			
			if ((day == day1.getDate()) || (day == day2.getDate()) || (day == day3.getDate()))
			{
				sdow=' bgcolor=#FFFFFF';
				can_set = true;
			}
			else sdow = ' bgcolor=#DDDDDD';	
			
			if ((mm-1) == curdate.getMonth())
			{
				if (day==curdate.getDate()) sdow=' bgcolor=#FFFF00';
				if (day > curdate.getDate())
				{
					can_set = false;
					sdow = ' bgcolor=#DDDDDD';
				}
			}
			
			if (weekday>=5) 
			{
				sdow = ' bgcolor=#C0C0C0';
			}	
			
			
			doc.write('<td'+sdow+'>\r\n');
			if (wd==wdstart) { 
					if (started==0)	started=1; 
				}; 
			if (started==1) {
				writeday(doc,this.id,yy,mm,day,wd, can_set);
				//doc.write(day);
				day++;
			} // if started
			else {
				doc.write('&nbsp;');
			}; // empty section
			if (day>27) {
				tempdate = new Date(yy,mm-1,day);
				//tempdate.setDate(day);
				if (tempdate.getDate()!=day) started=2;
				if (tempdate.getMonth()!=(mm-1)) started=2; 
				//if (tempdate.getYear()!=yy) started=2; 
			} // day > 27			
		} // for - weekday
		doc.write('</tr>');
		if (started==2) break; // not need empty week-line
	} // week
	doc.write('</table>');	
	doc.write('</body></html>');
	doc.close();
	
  obj_calwindow.opener = window;
  obj_calwindow.focus();
}